home *** CD-ROM | disk | FTP | other *** search
/ Java Certification Exam Guide / McGrawwHill-JavaCertificationExamGuide.iso / pc / Web Links and Code / code / chap27 / answer / EdnaUI.java < prev    next >
Encoding:
Java Source  |  1997-04-20  |  2.4 KB  |  101 lines

  1. package client;
  2.  
  3. import java.awt.*;
  4. import java.util.Date;
  5. import java.util.StringTokenizer;
  6. import java.io.IOException;
  7.  
  8. public class EdnaUI extends Panel implements CanUpdate {
  9.    private static final int MAX_SEATS = 5;
  10.  
  11.    private List      passengerList;
  12.    private Button    reserve;
  13.  
  14.    public EdnaUI() {
  15.       setLayout(new BorderLayout());
  16.  
  17.       Panel p;
  18.       p = new Panel();
  19.  
  20.       p.add(new Label("Date: 4/10/97"));
  21.       p.add(new Label("Flight #: 100"));
  22.  
  23.       add("North", p);
  24.       
  25.       passengerList = new List(5, false);
  26.       add("Center", passengerList);
  27.  
  28.       p = new Panel();
  29.       reserve = new Button("new reservation");
  30.       p.add(reserve);
  31.  
  32.       add("South", p);
  33.  
  34.    }
  35.  
  36.    /**
  37.     * Show frame to make a new reservation when the user clicks
  38.     * the "new reservation" button.
  39.     * Show frame to delete a reservation when the user double-clicks
  40.     * the list.
  41.     */
  42.    public boolean action(Event e, Object what) {
  43.       try {
  44.          if (e.target == reserve)
  45.             showNewWindow();
  46.          else if (e.target == passengerList)
  47.             showDeleteWindow();
  48.       } catch (IOException x) {
  49.          System.out.println("could not edit passenger list");
  50.       }
  51.  
  52.       return super.action(e, what);
  53.    }
  54.  
  55.    private void showDeleteWindow() throws IOException {
  56.       Frame pass;
  57.  
  58.       // Get the selected entry in the list, if there is one.
  59.       String name = passengerList.getSelectedItem();
  60.       if (name != null) {
  61.          pass = new DeletePassenger(name, this);
  62.          pass.pack();
  63.          pass.show();
  64.       }
  65.    }
  66.  
  67.    private void showNewWindow() throws IOException {
  68.       Frame pass;
  69.  
  70.       pass = new NewPassenger(this);
  71.       pass.pack();
  72.       pass.show();
  73.    }
  74.  
  75.  
  76.    public void updateUI() {
  77.       String[] reservations;
  78.       try {
  79.          reservations = new Client().getPassengerList();
  80.       } catch (IOException x) {
  81.          System.out.println("Unable to show list");
  82.          System.out.println(x.getMessage());
  83.          return;
  84.       }
  85.       passengerList.clear();
  86.  
  87.       int total = reservations.length;
  88.       for (int i = 0; i < total; i++) 
  89.          passengerList.addItem(reservations[i]);
  90.  
  91.       // Update the "make reservation" button as appropriate.
  92.       if (reservations.length == MAX_SEATS)
  93.          reserve.enable(false);
  94.       else
  95.          reserve.enable(true);
  96.  
  97.       validate();
  98.    }
  99.  
  100. }
  101.